<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/26
 * Time: 16:42
 */

namespace App\Http\Controllers\Api;

use Validator;
use Illuminate\Support\Facades\DB;
use App\Services\UserService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\ActivityService;
use App\Lib\Util\ResponseUtil;
use Illuminate\Support\Facades\Schema;
use App\Activity;

class ActivityController extends Controller
{
    private $activityService = null;

    public function __construct(ActivityService $activityService)
    {
        $this->activityService = $activityService;
    }

    /*
     * name:获取首页活动列表
     * author:hy
     * param:$request
     */
    public function getActivityList(Request $request)
    {
        $activities = $this->activityService
            ->getActivities($request->all(), [
                Activity::$AUDIT_STATUS_PERMITTED
            ]);

        return ResponseUtil::jsonResponse(true, 0, '', $activities);
    }

    public function getTotalTeamCount()
    {

    }

    /*
     * name:获取活动详情
     * author:hy
     * param:$request
     */
    public function getActivityDetail(Request $request)
    {
        $input = $request->all();
        $activities = $this->activityService
            ->getActivityDetail($input);
        return ResponseUtil::jsonResponse(true, 0, '', $activities);
    }

    /*
     * name:加入活动
     * author:hy
     * param:$request
     */
    public function joinActivityApply(Request $request)
    {
        $input = $request->all();

        $this->canAuditJoinActivity($input);

        $this->activityService->applyJoinActivity($input);

        return ResponseUtil::jsonResponse(true, 0, '', []);
    }

    /*
     * name:加入活动验证
     * author:hy
     * param:$request
     */
    public function canAuditJoinActivity(Array $input)
    {
        return Validator::make($input,[
            'activity_id' => [
                'required',
                'numeric'
            ],
            'user_id' =>[
                'required'
            ],
            'city_id'=>[
                'required'
            ]
        ], [
            'activity_id.required' => '必须传入活动ID',
            'activity_id.numeric' => '活动ID格式不正确，必须为数字',
            'user_id.required'  =>  '必须传入用户ID',
            'city_id.required'  =>  '必须传入城市ID',
        ])->after(function ($validator) use ($input) {
            if(isset($input['activity_id'])){
                $res = $this->activityService->activityIsOpenJoin($input['activity_id']);
                if (!$res) {
                    $validator->errors()->add('join_method.illegal', '该活动不允许开放参与');
                }
            }
            if(isset($input['activity_id']) && isset($input['user_id'])){
                $isJoin = $this->activityService->isHasBeingJoin($input['user_id'],$input['activity_id']);
                if($isJoin){
                    $validator->errors()->add('is_join.illegal', '已参与活动');
                }
            }
        })->validate();
    }
}
